package yy.yyds.org.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yy.yyds.basic.query.util.QueryResult;
import yy.yyds.org.domain.Department;
import yy.yyds.org.mapper.DepartmentMapper;
import yy.yyds.org.query.DepartmentQuery;
import yy.yyds.org.service.IDepartmentService;

import java.util.List;
@Service
/*读事务的注解*/
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)

public class DepartentServiceimpl implements IDepartmentService {

    @Autowired
    private  DepartmentMapper departmentMapper;

    @Override
    /*写事务*/
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
    }

    @Override
    /*写事务*/
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    /*写事务*/
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department loadOne(Long id) {
        return departmentMapper.loadOne(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();
    }

    /*分页查询实现*/
    @Override
    public QueryResult<Department> querypage(DepartmentQuery departmentQuery) {
        /*查询总条数*/
        Integer total = departmentMapper.queryCount(departmentQuery);
        if (total == 0){//如果总条数已经是0条 就不需要后面的查询
//            ctrl + alt + 向右或向左 反复横跳
            return new QueryResult<>();
        }else {
            /*查询当前页数据*/
            List<Department> departments = departmentMapper.queryList(departmentQuery);
            return new QueryResult<>(total,departments);
        }
    }

    @Override
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }

    @Override
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }
}
